<?php
class TagsAction extends AdminAction{
	//TAG列表
	public function index(){
		$tags=M('Tags');

		import('ORG.Util.Page');					//导入分页类
		$count=$tags->where()->count();			//获得符合条件的数据总数
		$Page=new Page($count,15);				//实例化分页类 传入数据总数		
		//定义分页样式
		$Page->setConfig('prev','上页');
		$Page->setConfig('next','下页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','末页');
		$Page->rollPage = 20;
		$Page->setConfig('theme','%first% %upPage% %linkPage% %downPage% %end%');
			 
		$show=$Page->show();						//分页显示输出

		$list=$tags->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);	//输出分页
		$this->display();
	}

	public function add(){
		$this->display();
	}
	public function adding(){
		$tags=D('Tags');
		if (!$tags->create()){
			$this->error('TAG已存在');
		}else{
			$res=$tags->add($_POST);
			if($res){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}
	//重新导入TAG标签
	public function inc(){	
		$this->display();
	}

	public function incing(){
		$Aattach=M('ArticleAttach');

		//导出所有TAG
		$where['tag']=array('neq','null');
		$tag['A']=$Aattach->where($where)->field('tag')->select();

		//过滤空TAG
		if($tag['A']){
			$tags=$tag['A'];
		}

		//拆分并过滤重复的TAG
		$data=array();$i=0;
		foreach($tags as $key=>$val){
			if(substr_count($val['tag'],',')>0){
				$list=explode(',',$val['tag']);
				foreach($list as $k=>$v){
					if($v==Null){
						unset($v);
					}else{
						$data[$i]=$v;
						$i++;
					}
				}
			}else{
				$data[$i]=$val['tag'];
				$i++;
			}
		}
		$data=array_unique($data);

		//如果TAG表中不存在则添加
		echo '<font style="font-size:14px;">蓝色关键词表示导入成功，红色关键词表示表中已存在</font><a href="index">返回</a><br/>';

		$i=0;
		foreach($data as $k=>$v){
			$_POST['tag']=$v;

			$tags=M('Tags');
			$result=$tags->where(array('tag'=>$v))->field('tag')->find();
			if($result){
				echo '<font style="font-size:12px;color:red">'.$_POST['tag'].',</font>';
			}else{
				$tags->add($_POST);
				echo '<font style="font-size:12px;color:blue">'.$_POST['tag'].',</font>';
			}
			if($i==5){echo '<br/>';}
			$i++;	
		}

	}

	//删除TAG
	public function del(){
		$tags=M('Tags');
		$data['tid']=$_GET['id'];
		$result=$tags->where($data)->delete();
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}



}